package action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import model.Person;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import service.PersonService;

import com.opensymphony.xwork2.ActionContext;

@Component
@Scope("prototype")
public class PersonAction {
	@Resource
	private PersonService personService;
	
	private Integer selectNo;
	private String updateName;
	private Person person;
	
	public Integer getSelectNo() {
		return selectNo;
	}

	public void setSelectNo(Integer selectNo) {
		this.selectNo = selectNo;
	}
	
	public String getUpdateName() {
		return updateName;
	}

	public void setUpdateName(String updateName) {
		this.updateName = updateName;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}	
	
	public String list(){
		fetchList();
		return "success";
	}

	public String add(){
		personService.add(person);
		fetchList();
		return "success";
	}
	
	public String delete(){
		Person p1=new Person();
		p1.setId(selectNo);
		personService.delete(p1);
		fetchList();
		return "success";		
	}
	
	public String update(){
		Person p1=new Person();
		p1.setId(selectNo);
		p1.setName(updateName);
		personService.update(p1);
		fetchList();
		return "success";		
	}	

	private void fetchList(){
		List<Person> personList=personService.listAll();
		Map request=(Map)ActionContext.getContext().get("request");
		request.put("personList", personList);
	}


}
